/**
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.zhongketech.sunflower.activity.me;

import com.zhongketech.sunflower.Constant;
import com.zhongketech.sunflower.MyApplication;
import com.zhongketech.sunflower.MyToast;
import com.zhongketech.sunflower.R;
import com.zhongketech.sunflower.activity.BaseActivity;
import com.zhongketech.sunflower.activity.LoginActivity;
import com.zhongketech.sunflower.control.SpecialNumsManager;
import com.zhongketech.sunflower.transport.Action;
import com.zhongketech.sunflower.transport.LogUtil;
import com.zhongketech.sunflower.transport.model.Authorizations;
import com.zhongketech.sunflower.transport.model.ImageUrl;
import com.zhongketech.sunflower.transport.model.SmsCode;
import com.zhongketech.sunflower.transport.model.User;
import com.zhongketech.sunflower.transport.uitl.NetTransportUtil;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


/**
 * 注册页
 * 
 */
@SuppressLint("HandlerLeak")
public class ModifyPhoneActivity extends BaseActivity implements OnClickListener {
	private EditText phoneEditText;
	private EditText smsCodeEditText;
	private EditText passwordEditText;

	private boolean isHide = true;
	private ImageView passwordCtr;
	private Handler handler ;

	private static final int SETSMSCODE = 0;
	private static final int UPDATESUCC = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_modify_phone);


		handler = new Handler() {
			public void handleMessage(Message msg) {

				switch (msg.arg1) 
				{
				case SETSMSCODE:
					smsCodeEditText.setText((CharSequence) msg.obj);
					break;
				case UPDATESUCC:
					finish();
					break;
				default:
					break;
				}
			}
		};

		initComponent();
		initTitle();
	}

	private void initComponent() {
		phoneEditText = (EditText) findViewById(R.id.edit_phone_number);
		smsCodeEditText = (EditText) findViewById(R.id.edit_smsCode);
		passwordEditText = (EditText) findViewById(R.id.edit_password);
		passwordCtr = (ImageView) findViewById(R.id.passwd_ctr);		

		// 移动光标到文字后面
		//		phoneEditText.setSelection(phoneEditText.getHint().length());
		//		smsCodeEditText.setSelection(smsCodeEditText.getHint().length());
		//		passwordEditText.setSelection(passwordEditText.getHint().length());
	}

	private void initTitle() {
		//		TextView back = (TextView) findViewById(R.id.back);
		TextView title = (TextView) findViewById(R.id.title);
		//		TextView other =(TextView) findViewById(R.id.other);

		//		back.setOnClickListener(this);
		title.setText("修改电话号码");
		//		other.setVisibility(View.VISIBLE);
		//		other.setText(getResources().getString(R.string.save));
		//		other.setOnClickListener(this);
	}

	/**
	 * 获取验证码
	 * 
	 * @param view
	 */
	public void getSmsCode(View view) {
		final String phone = phoneEditText.getText().toString().trim();
		if (TextUtils.isEmpty(phone)) {
			phoneEditText.setError("手机号不能为空");
			phoneEditText.requestFocus();
		} else if (phone.length() < 11 || (!phone.startsWith("1"))) {
			phoneEditText.setError("请输入正确的手机号");
			phoneEditText.requestFocus();
		} else {
			final ProgressDialog pd = new ProgressDialog(this);
			pd.setCanceledOnTouchOutside(false);
			pd.setMessage("获取...");
			pd.show();

			new Thread(new Runnable() {
				public void run() {
					Looper.prepare();
					SmsCode sc = new Action().smsCode(phone);
					pd.dismiss();
					if(null != sc)
					{
						final Message msg = new Message();
						msg.arg1 = SETSMSCODE;
						msg.obj = sc.getSmscode();
						handler.sendMessage(msg);

					}
				}
			}).start();
		}
	}

	/**
	 * 失去焦点
	 */
	public void loseFocus(View view) {
		view.setFocusable(true);
		view.setFocusableInTouchMode(true);
		view.requestFocus();
		InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(phoneEditText.getWindowToken(), 0);
	}

	/**
	 * 改变密码状态
	 * 
	 * @param view
	 */
	public void passwdStateChange(View view) {
		if (isHide) {
			isHide = false;
			passwordCtr.setBackgroundResource(R.drawable.show_password);
			passwordEditText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
		} else {
			isHide = true;
			passwordCtr.setBackgroundResource(R.drawable.hide_password);
			passwordEditText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
		}
	}

	/**
	 * 完成
	 * 
	 * @param view
	 */
	public void modifyPhoneComplete(View v) {
		final String phone = phoneEditText.getText().toString().trim();
		final String smsCode = smsCodeEditText.getText().toString().trim();
		final String password = passwordEditText.getText().toString().trim();
		if (TextUtils.isEmpty(phone)) {
			phoneEditText.setError("手机号不能为空");
			phoneEditText.requestFocus();
			return;
		} else if (TextUtils.isEmpty(smsCode)) {
			smsCodeEditText.setError("验证码不能为空");
			smsCodeEditText.requestFocus();
			return;
		} else if (TextUtils.isEmpty(password)) {
			passwordEditText.setError("密码不能为空");
			passwordEditText.requestFocus();
			return;
		}

		final ProgressDialog pd = new ProgressDialog(this);
		pd.setCanceledOnTouchOutside(false);
		pd.setMessage("正在更改...");
		pd.show();

		new Thread(new Runnable() {
			public void run() {
				Looper.prepare();
				User newUser = new Action().modifyPhone(phone, smsCode, password);
				pd.dismiss();
				if(null != newUser)
				{
					MyApplication.getInstance().setPhone(newUser.getPhone());
					MyToast.getInstance().show("更新成功");
					final Message msg = new Message();
					msg.arg1 = UPDATESUCC;
					handler.sendMessage(msg);
				}
			}
		}).start();
	}



	public void backHandle() {
		InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(phoneEditText.getWindowToken(), 0);
		finish();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		//		case R.id.back:
		//			backHandle();
		//			break;
		case R.id.passwd_ctr:
			if (isHide) {
				isHide = false;
				passwordCtr.setBackgroundResource(R.drawable.hide_password);
				passwordEditText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
			} else {
				isHide = true;
				passwordCtr.setBackgroundResource(R.drawable.show_password);
				passwordEditText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_TEXT_VARIATION_PASSWORD);
			}
			break;
			//		case R.id.other:
			//			modifyPhoneComplete();
			//			break;
		default:
			break;
		}
	}

}
